package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JProgressBar;
import javax.swing.JTextPane;
import javax.swing.SwingWorker;

import model.BinForm;
import model.BinSetVariables;
/**
 * 
 * @author Estelle && Marie
 * This class is used for implements the action listener of the button Start Simulation without thread 
 * where is in the mainWindow
 *
 */
public class StartSimulation implements ActionListener{

	JTextPane text;
	BinForm jt;
	BinSetVariables bsv = new BinSetVariables();	
	JProgressBar barre;
	/**
	 * Constructor of the Class
	 * @param jtextfields
	 * @param text
	 * @param bar
	 */
	public StartSimulation(BinForm jtextfields,JTextPane text, JProgressBar bar){
		this.jt = jtextfields;
		this.text = text ;
		this.barre = bar;
		
	}
	
	/**
	 * we implement the action listener
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		/**
		 *  we get the value of the contents of each JTextFields of the binform which are filled in clicking on 
		 *  a element of the list or in filling each JTextFields
		 */
		
		if(jt.allIsFilled()){
			bsv.setCallPutFlag(jt.getCallPutFlag().getText());
			bsv.setS(Double.valueOf(jt.getS().getText()));
			bsv.setX(Double.valueOf(jt.getX().getText()));
			bsv.setT(Double.valueOf(jt.getT().getText()));
			bsv.setR(Double.valueOf(jt.getR().getText()));
			bsv.setB(Double.valueOf(jt.getB().getText()));
			bsv.setV(Double.valueOf(jt.getV().getText()));
			bsv.setnSteps(Integer.valueOf(jt.getnSteps().getText()));
			bsv.setnSimulations(Integer.valueOf(jt.getnSimulations().getText()));
			
			
			text.setText("the simulation has been launched");
			
			
			
			/**
			 * we use a swingWorker for executing the simulation and for refreshing our mainWindow 
			 * and so our progress bar
			 */
			
			(new SwingWorker(){

				@Override
				protected Object doInBackground() throws Exception {
					// TODO Auto-generated method stub
					barre.setValue(0);
					new WithoutThread(bsv, text, barre);
					
					
					return null;
				}
				
				
			}).execute();
		
		}
		else text.setText("\t ! WARNING !: \nYou must set all variables before launching the simulation");
		
		
	}
}
